﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Ryder_US_Website.BUS;
using System.Collections.Generic;

namespace Ryder_US_Website.Presentation.Admin
{
    public partial class Cargo_Insert : System.Web.UI.Page
    {
        
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Session["RUS_User"] == null)
                {
                    Response.Redirect("Login.aspx");
                }
                // Load Service
                List<RUS_ServiceDetail> service = ServiceDetailBUS.GetAll();
                DropDownListTenDichVu.DataSource = service;
                DropDownListTenDichVu.DataTextField = "ServiceDetail_name";
                DropDownListTenDichVu.DataValueField = "ServiceDetail_ID";
                DropDownListTenDichVu.DataBind();

                //Load Customer
                List<RUS_Customer> customer = CustomerBUS.GetAll();
                DropDownListCustomer.DataSource = customer;
                DropDownListCustomer.DataTextField = "Customer_name";
                DropDownListCustomer.DataValueField = "Customer_ID";
                DropDownListCustomer.DataBind();
                
            }
        }

        protected void clean()
        {
            txtSoLuong.Text = "";
            txtNguoiNhan.Text = "";
            txtDiaChi.Text = "";
            txtDienThoai.Text = "";
            txtGhiChu.Text = "";
            txtTongTien.Text = "";
            txtSoTienTra.Text = "";
        }
        protected void btnHuy_Click(object sender, EventArgs e)
        {
            Response.Redirect("Cargo_List.aspx");
        }

        protected void btnLamLai_Click(object sender, EventArgs e)
        {
            clean();
        }

        protected void btnThem_Click(object sender, EventArgs e)
        {
            RUS_Cargo cargo = new RUS_Cargo();
            cargo.Cargo_serviceDetailID = Int16.Parse(DropDownListTenDichVu.SelectedItem.Value);
            cargo.Cargo_count = Int16.Parse(txtSoLuong.Text);
            cargo.Cargo_sender = Int16.Parse(DropDownListCustomer.SelectedItem.Value);
            cargo.Cargo_receiver = txtNguoiNhan.Text;
            cargo.Cargo_receivePhone = txtDienThoai.Text;
            cargo.Cargo_receiveAddress = txtDiaChi.Text;
            cargo.Cargo_description = txtGhiChu.Text;
            cargo.Cargo_total = Decimal.Parse(txtSoTienTra.Text);
            cargo.Cargo_paidMoney = Decimal.Parse(txtSoTienTra.Text);
            try
            {
                cargo = CargoBUS.Insert(cargo.Cargo_serviceDetailID,cargo.Cargo_count,cargo.Cargo_sender,cargo.Cargo_receiver,cargo.Cargo_receivePhone,cargo.Cargo_receiveAddress,
                                        cargo.Cargo_description,cargo.Cargo_total,cargo.Cargo_paidMoney);
            }
            catch(Exception ex)
            {
                Response.Write(Utils.UtilHelpers.ShowMessage(ex.Message));
            }
            if (cargo.Cargo_serviceDetailID!=0)
            {
                Response.Write(Utils.UtilHelpers.ShowMessage("Thêm đơn hàng thành công"));
                Response.Redirect("Cargo_List.aspx");
            }
            else
            {
                Response.Write(Utils.UtilHelpers.ShowMessage("Thêm đơn hàng thất bại"));
            }
        }
    }
}
